﻿using Dapper;
using OMG.Fill.Potato.Model.Ent;
using OMG.Fill.Potato.Service;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Potato.Services
{
    /// <summary>
    /// 各生产季马铃薯生产情况统计表通用操作类
    /// </summary>
    public class PotSeasonProducts
    {
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(List<PotSeasonProductEntity> list) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_PotSeasonProduct";
            strSql +="(";
            strSql +="Id,";
            strSql +="ReportId,";
            strSql +="SeasonDict,";
            strSql +="SeasonValue,";
            strSql +="PseasonValue,";
            strSql +="PHTime,";
            strSql +="MainVariety,";
            strSql +="TotalArea,";
            strSql +="TotalYield,";
            strSql +="AvgMuYield,";
            strSql +="MuOutput,";
            strSql +="TotalOutput,";
            strSql +="TechModel,";
            strSql += "tdSeedArea,";
            strSql +="SeedCoverage,";
            strSql +="MulchArea,";
            strSql +="DripArea,";
            strSql +="FertilizerArea,";
            strSql +="NoCultivationArea,";
            strSql +="JZArea,";
            strSql += "JZYield,";
            strSql +="JZAvgMuYield,";
            strSql +="JZMuOutput,";
            strSql +="JZDensity,";
            strSql +="JZTechModel,";
            strSql +="JTArea,";
            strSql += "JTYield,";
            strSql +="JTAvgMuYield,";
            strSql +="JTMixMuYield,";
            strSql +="JTMixMuOutput,";
            strSql +="JTTechModel,";
            strSql +="MechPlantArea,";
            strSql +="MechCultivationArea,";
            strSql +="MechFertilizerArea,";
            strSql +="MechPesticideArea,";
            strSql +="MechHarvestArea,";
            strSql +="SeedCost,";
            strSql +="FertilizerCost,";
            strSql +="PesticideCost,";
            strSql +="MulchCost,";
            strSql +="LaborCost,";
            strSql +="OtherCost,";
            strSql +="TotalCost,";
            strSql +="OAvgMuYield,";
            strSql +="OAvgPrice,";
            strSql +="OTotalOutput,";
            strSql +="Income)";
            strSql +=" values(";
            strSql +="@Id,";
            strSql +="@ReportId,";
            strSql +="@SeasonDict,";
            strSql +="@SeasonValue,";
            strSql +="@PseasonValue,";
            strSql +="@PHTime,";
            strSql +="@MainVariety,";
            strSql +="@TotalArea,";
            strSql +="@TotalYield,";
            strSql +="@AvgMuYield,";
            strSql +="@MuOutput,";
            strSql +="@TotalOutput,";
            strSql +="@TechModel,";
            strSql += "@tdSeedArea,";
            strSql +="@SeedCoverage,";
            strSql +="@MulchArea,";
            strSql +="@DripArea,";
            strSql +="@FertilizerArea,";
            strSql +="@NoCultivationArea,";
            strSql +="@JZArea,";
            strSql += "@JZYield,";
            strSql +="@JZAvgMuYield,";
            strSql +="@JZMuOutput,";
            strSql +="@JZDensity,";
            strSql +="@JZTechModel,";
            strSql +="@JTArea,";
            strSql += "@JTYield,";
            strSql +="@JTAvgMuYield,";
            strSql +="@JTMixMuYield,";
            strSql +="@JTMixMuOutput,";
            strSql +="@JTTechModel,";
            strSql +="@MechPlantArea,";
            strSql +="@MechCultivationArea,";
            strSql +="@MechFertilizerArea,";
            strSql +="@MechPesticideArea,";
            strSql +="@MechHarvestArea,";
            strSql +="@SeedCost,";
            strSql +="@FertilizerCost,";
            strSql +="@PesticideCost,";
            strSql +="@MulchCost,";
            strSql +="@LaborCost,";
            strSql +="@OtherCost,";
            strSql +="@TotalCost,";
            strSql +="@OAvgMuYield,";
            strSql +="@OAvgPrice,";
            strSql +="@OTotalOutput,";
            strSql +="@Income)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    var trans = dbCon.BeginTransaction();

                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].Id = CustomFun.GetGuid();
                    }

                    string strDel = "delete from dbo.T_PotSeasonProduct where ReportId=@ReportId";
                    dbCon.Execute(strDel, new { ReportId = list[0].ReportId }, trans);
                    int result = dbCon.Execute(strSql, list, trans);
                    string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                    dbCon.Execute(strRep, new { ReportId = list[0].ReportId, UpdateTime = DateTime.Now }, trans);
                    //提交事务
                    trans.Commit();
                    obj.model = result;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "更新成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增各生产季马铃薯生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(PotSeasonProductEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.T_PotSeasonProduct set ";
            strSql+="ReportId = @ReportId,";
            strSql+="SeasonDict = @SeasonDict,";
            strSql+="SeasonValue = @SeasonValue,";
            strSql+="PseasonValue = @PseasonValue,";
            strSql+="PHTime = @PHTime,";
            strSql+="MainVariety = @MainVariety,";
            strSql+="TotalArea = @TotalArea,";
            strSql+="TotalYield = @TotalYield,";
            strSql+="AvgMuYield = @AvgMuYield,";
            strSql+="MuOutput = @MuOutput,";
            strSql+="TotalOutput = @TotalOutput,";
            strSql+="TechModel = @TechModel,";
            strSql+="SeedCoverage = @SeedCoverage,";
            strSql+="MulchArea = @MulchArea,";
            strSql+="DripArea = @DripArea,";
            strSql+="FertilizerArea = @FertilizerArea,";
            strSql+="NoCultivationArea = @NoCultivationArea,";
            strSql+="JZArea = @JZArea,";
            strSql+="JZAvgMuYield = @JZAvgMuYield,";
            strSql+="JZMuOutput = @JZMuOutput,";
            strSql+="JZDensity = @JZDensity,";
            strSql+="JZTechModel = @JZTechModel,";
            strSql+="JTArea = @JTArea,";
            strSql+="JTAvgMuYield = @JTAvgMuYield,";
            strSql+="JTMixMuYield = @JTMixMuYield,";
            strSql+="JTMixMuOutput = @JTMixMuOutput,";
            strSql+="JTTechModel = @JTTechModel,";
            strSql+="MechPlantArea = @MechPlantArea,";
            strSql+="MechCultivationArea = @MechCultivationArea,";
            strSql+="MechFertilizerArea = @MechFertilizerArea,";
            strSql+="MechPesticideArea = @MechPesticideArea,";
            strSql+="MechHarvestArea = @MechHarvestArea,";
            strSql+="SeedCost = @SeedCost,";
            strSql+="FertilizerCost = @FertilizerCost,";
            strSql+="PesticideCost = @PesticideCost,";
            strSql+="MulchCost = @MulchCost,";
            strSql+="LaborCost = @LaborCost,";
            strSql+="OtherCost = @OtherCost,";
            strSql+="TotalCost = @TotalCost,";
            strSql+="OAvgMuYield = @OAvgMuYield,";
            strSql+="OAvgPrice = @OAvgPrice,";
            strSql+="OTotalOutput = @OTotalOutput,";
            strSql+="Income = @Income";
            strSql+=" where Id = @Id";
           try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新各生产季马铃薯生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_PotSeasonProduct where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除各生产季马铃薯生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_PotSeasonProduct where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除各生产季马铃薯生产情况统计表表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public  ResResult<List<PotSeasonProductEntity>> GetModel(string ReportId,string SeasonValue) {
            ResResult<List<PotSeasonProductEntity>> obj = new ResResult<List<PotSeasonProductEntity>>();
            List<PotSeasonProductEntity> list = new List<PotSeasonProductEntity>();
            ReportServices reportService = new ReportServices();
            string strSql = @"select * from dbo.T_PotSeasonProduct where ReportId = @ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (reportService.CheckRepCount(ReportId, "T_PotSeasonProduct"))
                    {
                        if (SeasonValue.Equals("3"))
                        {
                            string[] sdicts = { "冬作", "小春作", "早春作"};
                            string[] svalues = { "3-1", "3-2", "3-3" };
                            for (int i = 0; i < 3; i++)
                            {
                                PotSeasonProductEntity entity = new PotSeasonProductEntity();
                                entity.ReportId = ReportId;
                                entity.SeasonDict = sdicts[i];
                                entity.SeasonValue = svalues[i];
                                list.Add(entity);                               
                            }                            
                        }
                        else
                        {
                            PotSeasonProductEntity entity = new PotSeasonProductEntity();
                            entity.ReportId = ReportId;
                            entity.SeasonDict = DataDict.Instance["QuarterCycleValue"][SeasonValue];
                            entity.SeasonValue = SeasonValue;
                            list.Add(entity);
                        }
                        
                    }
                    else
                    {
                        list = dbCon.Query<PotSeasonProductEntity>(strSql, new { ReportId = ReportId }).ToList();
                    }
                    obj.model = list;
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取各生产季马铃薯生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<PotSeasonProductEntity>> GetList()
        {
            ResResult<List<PotSeasonProductEntity>> obj = new ResResult<List<PotSeasonProductEntity>>();
            string strSql = @"select * from dbo.T_PotSeasonProduct";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<PotSeasonProductEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新各生产季马铃薯生产情况统计表对象信息失败", ex);
                return obj;
            }
        }
    }
}